package com.venutech.gongan.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.venutech.gongan.dao.QdsxjMapper;
import com.venutech.gongan.dao.TbWorkMapper;
import com.venutech.gongan.dao.TbWorkOrderMapper;
import com.venutech.gongan.entity.Qdsxj;
import com.venutech.gongan.entity.TbWork;
import com.venutech.gongan.entity.TbWorkOrder;
import com.venutech.gongan.service.ITbWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 巡检Service业务层处理
 * 
 * @author linxb
 * @date 2023-09-18
 */
@Service
public class TbWorkServiceImpl implements ITbWorkService 
{
    @Autowired
    private TbWorkMapper tbWorkMapper;
    @Autowired
    private QdsxjMapper qdsxjMapper;
    @Autowired
    private TbWorkOrderMapper tbWorkOrderMapper;
    @Autowired
    private TbWorkMapper workMapper;


    /**
     * 查询巡检
     * 
     * @param wkId 巡检主键
     * @return 巡检
     */
    @Override
    public TbWork selectTbWorkByWkId(Long wkId)
    {
        return tbWorkMapper.selectTbWorkByWkId(wkId);
    }

    /**
     * 查询巡检列表
     * 
     * @param tbWork 巡检
     * @return 巡检
     */
    @Override
    public List<TbWork> selectTbWorkList(TbWork tbWork)
    {
        return tbWorkMapper.selectTbWorkList(tbWork);
    }

    /**
     * 新增巡检
     * 
     * @param tbWork 巡检
     * @return 结果
     */
    @Override
    public int insertTbWork(TbWork tbWork)
    {
        return tbWorkMapper.insertTbWork(tbWork);
    }

    /**
     * 修改巡检
     * 
     * @param tbWork 巡检
     * @return 结果
     */
    @Override
    public int updateTbWork(TbWork tbWork)
    {
        return tbWorkMapper.updateTbWork(tbWork);
    }

    /**
     * 批量删除巡检
     * 
     * @param wkIds 需要删除的巡检主键
     * @return 结果
     */
    @Override
    public int deleteTbWorkByWkIds(String wkIds)
    {
        return tbWorkMapper.deleteTbWorkByWkIds(Convert.toStrArray(wkIds));
    }

    /**
     * 删除巡检信息
     * 
     * @param wkId 巡检主键
     * @return 结果
     */
    @Override
    public int deleteTbWorkByWkId(Long wkId)
    {
        return tbWorkMapper.deleteTbWorkByWkId(wkId);
    }

    @Override
    public List<TbWork> selectcompletedTbWorkList(TbWork tbWork) {
        return tbWorkMapper.selectcompletedTbWorkList(tbWork);
    }

    @Override
    public List<TbWork> faultyWorkList(TbWork tbWork) {
        return tbWorkMapper.faultyWorkList(tbWork);
    }

    @Override
    public List<TbWork> intelWorkList(TbWork tbWork) {
        return tbWorkMapper.intelWorkList(tbWork);
    }

    @Override
    public List<TbWork> faultyIntelWorkList(TbWork tbWork) {
        return tbWorkMapper.faultyIntelWorkList(tbWork);
    }

    @Override
    public void regularInspection() {
        List<Qdsxj> qdsxjList=qdsxjMapper.getAllFault();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        for(Qdsxj qdsxj:qdsxjList){
            TbWorkOrder tbWorkXJOrder=new TbWorkOrder();
            tbWorkXJOrder.setWkSbId((long)qdsxj.getSb_id());
            tbWorkXJOrder.setWkState((long)0);
            List<TbWorkOrder> tbWorkOrderList=tbWorkOrderMapper.selectTbWorkXJOrderList(tbWorkXJOrder);
            if(tbWorkOrderList.size()<0){
                TbWorkOrder tbWorkOrder=new TbWorkOrder();
                tbWorkOrder.setWkSbId((long)qdsxj.getSb_id());
                tbWorkOrder.setFaultType((long)9);//4小时巡检
                tbWorkOrder.setWkState((long)0);
                tbWorkOrder.setWkOrderType(2);//故障工单
//            work.setWkOrderType(1);//巡检工单
                tbWorkOrder.setWkAreaCode(qdsxj.getSb_xzqy());
                tbWorkOrder.setWkSqTime(date);
//            tbWorkOrder.setWkSqUserId(Long.parseLong(qdsxj.getSb_cjry()));
//            tbWorkOrder.setWkSpUserId(Long.parseLong(qdsxj.getSb_whry()));

//            tbWorkOrder.setFlowMethod("第三方平台");
                tbWorkOrderMapper.insertTbWorkOrder(tbWorkOrder);
                TbWork work=new TbWork();
                work.setWkOrderId(tbWorkOrder.getWkId());
                work.setWkSbId((long)qdsxj.getSb_id());
                work.setFaultType((long)9);//4小时巡检
                work.setWkState((long)0);
                work.setWkOrderType(2);//故障工单
//            work.setWkOrderType(1);//巡检工单
                work.setWkAreaCode(qdsxj.getSb_xzqy());
                work.setWkSqTime(date);
//            work.setWkSqUserId(Long.parseLong(qdsxj.getSb_cjry()));
//            work.setWkSpUserId(Long.parseLong(qdsxj.getSb_whry()));

//            work.setFlowMethod("第三方平台");
                workMapper.insertTbWork(work);
            }

        }
    }
}
